<?php

/**
 * Uploatfile clas of Project
 * @category Extend
 * @package Util
 * @author ThaiNM <minhthaind@gmail.com>
 * @version 1.0
 * @link URL description
 */
require_once dirname(__FILE__) . '/.././Config/Upload.php';

class UploadFile
{
    private $_file;
    private $_uploadfile;
    private $_status;

    public function __construct()
    {
        $this->_status = 0;
        $this->_file = isset($_FILES['file_contents']) ? $_FILES['file_contents'] : null;
    }

    /**
     * Get File Name
     * @return string fileName
     */
    public function getFileName()
    {
        return isset($this->_file['name']) ? basename($this->_file['name']) : '';
    }

    /**
     * Get File Type
     * @return String
     */
    public function getFileType()
    {
        return isset($this->_file['type']) ? $this->_file['type'] : '';
    }

    /**
     * Get File Error
     * @return String
     */
    public function getFileError()
    {
        return isset($this->_file['error']) ? $this->_file['error'] : 0;
    }

    /**
     * Get File Size
     * @return int
     */
    public function getFileSize()
    {
        return isset($this->_file['size']) ? $this->_file['size'] : 0;
    }

    /**
     * Get Status
     * @return int
     */
    public function getStatus()
    {
        return $this->_status;
    }

    /**
     * Get File Tmp
     * @return String
     */
    public function getFileTmp()
    {
        return isset($this->_file['tmp_name']) ? $this->_file['tmp_name'] : 0;
    }

    /**
     * Check File Size
     * @return bolean
     */
    public function checkFileSize()
    {
        return self::getFileSize() > FILE_SIZE;
    }

    /**
     * Check File Type Ext
     * @return bolean
     */
    public function checkFileTypeExt()
    {
        $arrFileTypeExt = split(',', FILE_TYPE_EXT);
        $fileName = self::getFileName();
        $ret = true;
        if ($fileName != '') {
            $temp = explode(".", $fileName);
            $extension = end($temp);
            $ret = in_array($extension, $arrFileTypeExt);
        }

        return $ret;
    }

    /**
     * Check File Type
     * @return Bolean
     */
    public function checkFileType()
    {
        $arrFileType = split(',', FILE_TYPE);
        $fileType = self::getFileType();
        $ret = true;
        if ($fileType != '') {
            $ret = in_array($fileType, $arrFileType);
        }

        return $ret;
    }

    /**
     * Check Error
     * @return bolean true, false
     */
    public function checkErr()
    {
        return self::getFileError() > 0;
    }

    /**
     * Set url File
     * @param string $url
     */
    public function setUrlFile($url)
    {
        $this->_uploadfile = $url . self::getFileName();
    }

    /**
     * Upload file to Server
     * @param  string  $url
     * @return boolean
     */
    public function uploadFile($url)
    {
        if (self::checkErr()) {
            $this->_status = 1;

            return false;
        }
        if (!self::checkFileTypeExt()) {
            $this->_status = 2;

            return false;
        }

        if (self::checkFileSize()) {
            $this->_status = 3;

            return false;
        }
        try {
            self::setUrlFile($url);
            $fileTmp = self::getFileTmp();
            if (move_uploaded_file($fileTmp, $this->_uploadfile)) {
                $this->_status = 0;
            } else {

                $this->_status = 4;

                return false;
            }
        } catch (Exception $ex) {
            echo $ex->getMessage();
            $this->_status = 5;

            return false;
        }

        return true;
    }

}
